#!/usr/bin/env python
#! -*- coding: utf-8 -*-

import sqlalchemy
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, SmallInteger, String, Float, Text, Date
from sqlalchemy.ext.declarative import declarative_base

from models.mongo.SFLZ_StdMedicine import SFLZ_StdMedicine

ModelBase = declarative_base()

class StdMedicine(ModelBase):
    """
    标准药品库信息
    """

    __tablename__ = 'std_medicine'

    id = Column('id', Integer, autoincrement=True)
    ypid = Column('ypid', String(32), primary_key=True)
    name = Column('name', String(255))
    manufacture = Column('manufacture', Text)
    enterprise_no = Column('enterprise_no', String(50))
    approval_no = Column('approval_no', String(50))
    category = Column('category', String(64))
    category_code = Column('category_code', String(32))
    phar_efficacy = Column('phar_efficacy', String(200))
    phar_efficacy_code = Column('phar_efficacy_code', String(16))
    varity_name_cn = Column('varity_name_cn', String(255))
    varity_name_en = Column('varity_name_en', String(500))
    varity_name_code = Column('varity_name_code', String(32))
    acid_base = Column('acid_base', String(80))
    acid_base_code = Column('acid_base_code', String(16))
    dosage_form = Column('dosage_form', String(64))
    dosage_form_code = Column('dosage_form_code', String(16))
    specification = Column('specification', String(200))
    formulation_spec_code = Column('formulation_spec_code', String(16))
    factor_coeff = Column('factor_coeff', Float)
    factor_coeff_code = Column('factor_coeff_code', String(16))
    material_packaging = Column('material_packaging', String(200))
    min_packaging_unit = Column('min_packaging_unit', String(16))
    min_preparation_unit = Column('min_preparation_unit', String(16))
    difference_note = Column('difference_note', Text)
    remark_code = Column('remark_code', String(16))
    imported_patented_drug = Column('imported_patented_drug', SmallInteger)
    original_drug = Column('original_drug', SmallInteger)
    low_priced_drug = Column('low_priced_drug', SmallInteger)
    fixed_prod_drug = Column('fixed_prod_drug', SmallInteger)
    original_prod_company_name = Column('original_prod_company_name', Text)
    original_ennterprise_code = Column('original_ennterprise_code', String(50))
    original_approval_no = Column('original_approval_no', String(50))
    approval_start_date = Column('approval_start_date', Date)
    approval_cancelled_date = Column('approval_cancelled_date', Date)
    ypid3 = Column('ypid3', String(32))
    ypid_change = Column('ypid_change', SmallInteger)
    modify_record = Column('modify_record', Text)
    theory_base09_drug = Column('theory_base09_drug', SmallInteger)
    base_09drug = Column('base_09drug', SmallInteger)
    theory_base12_drug = Column('theory_base12_drug', SmallInteger)
    base_12drug = Column('base_12drug', SmallInteger)
    base_12drug_category = Column('base_12drug_category', String(64))
    medical_insurance09_flag = Column('medical_insurance09_flag', SmallInteger)
    medical_insurance17_version = Column('medical_insurance17_version', String(32))
    numbering = Column('numbering', String(32))
    drug_classification = Column('drug_classification', String(100))
    drug_classification_code = Column('drug_classification_code', String(32))
    classification_primary = Column('classification_primary', String(64))
    classification_secondary = Column('classification_secondary', String(64))
    classification_thirdary = Column('classification_thirdary', String(64))
    classification_fourth = Column('classification_fourth', String(64))
    remark = Column('remark', String(255))


def formatConnectionString(dbconf):
    engine = dbconf['connector'] if 'connector' in dbconf else 'mysql'
    user = dbconf['user'] if 'user' in dbconf else 'guest'
    pwd = dbconf['pwd'] if 'pwd' in dbconf else ''
    host = dbconf['host'] if 'host' in dbconf else 'localhost'
    port = dbconf['port'] if 'port' in dbconf else 3306
    dbname = dbconf['db'] if 'db' in dbconf else 'guest'
    driverpart = '+'+ dbconf['driver'] if 'driver' in dbconf else ''

    return '%s%s://%s:%s@%s:%d/%s' % (engine, driverpart, user, pwd, host, port, dbname)

def query():
    db_bencao_app = {
        "connector": "postgresql",
        # driver: pymssql,
        "host": "10.10.61.37",
        "port": 5432,
        "user": "opsmart",
        "pwd": "q1w2e3r4",
        "db": "bencao_app"
    }

    engine_bencao_app = sqlalchemy.create_engine(formatConnectionString(db_bencao_app), 
                encoding=db_bencao_app['encoding'] if 'encoding' in db_bencao_app else 'utf-8',
                pool_pre_ping=True)

    DbSessionBencao = sessionmaker(bind=engine_bencao_app, autoflush=True, autocommit=False, expire_on_commit=True)

    db = DbSessionBencao()

    std_medicine = db.query(StdMedicine)
    save(std_medicine)

def getdata(data):
    if data is None:
        return ''
    return data

def save(data_list):
    print 'save..........'
    i = 0
    for data in data_list:
        i += 1
        medicine = SFLZ_StdMedicine(
            sn = int(data.id),
            ypid = getdata(data.ypid),
            name = getdata(data.name),
            manufacture = getdata(data.manufacture),
            enterprise_no = getdata(data.enterprise_no),
            approval_no = getdata(data.approval_no),
            category = getdata(data.category),
            category_code = getdata(data.category_code),
            phar_efficacy = getdata(data.phar_efficacy),
            phar_efficacy_code = getdata(data.phar_efficacy_code),
            varity_name_cn = getdata(data.varity_name_cn),
            varity_name_en = getdata(data.varity_name_en),
            varity_name_code = getdata(data.varity_name_code),
            acid_base = getdata(data.acid_base),
            acid_base_code = getdata(data.acid_base_code),
            dosage_form = getdata(data.dosage_form),
            dosage_form_code = getdata(data.dosage_form_code),
            specification = getdata(data.specification),
            formulation_spec_code = getdata(data.formulation_spec_code),
            factor_coeff = getdata(data.factor_coeff),
            factor_coeff_code = getdata(data.factor_coeff_code),
            material_packaging = getdata(data.material_packaging),
            min_packaging_unit = getdata(data.min_packaging_unit),
            min_preparation_unit = getdata(data.min_preparation_unit),
            difference_note = getdata(data.difference_note),
            remark_code = getdata(data.remark_code),
            imported_patented_drug = data.imported_patented_drug,
            original_drug = data.original_drug,
            low_priced_drug = data.low_priced_drug,
            fixed_prod_drug = data.fixed_prod_drug,
            original_prod_company_name = getdata(data.original_prod_company_name),
            original_ennterprise_code = getdata(data.original_ennterprise_code),
            original_approval_no = getdata(data.original_approval_no),
            approval_start_date = data.approval_start_date,
            approval_cancelled_date = data.approval_cancelled_date,
            ypid3 = getdata(data.ypid3),
            ypid_change = data.ypid_change,
            modify_record = getdata(data.modify_record),
            theory_base09_drug = data.theory_base09_drug,
            base_09drug = data.base_09drug,
            theory_base12_drug = data.theory_base12_drug,
            base_12drug = data.base_12drug,
            base_12drug_category = getdata(data.base_12drug_category),
            medical_insurance09_flag = data.medical_insurance09_flag,
            medical_insurance17_version = data.medical_insurance17_version,
            numbering = getdata(data.numbering),
            drug_classification = getdata(data.drug_classification),
            drug_classification_code = getdata(data.drug_classification_code),
            classification_primary = getdata(data.classification_primary),
            classification_secondary = getdata(data.classification_secondary),
            classification_thirdary = getdata(data.classification_thirdary),
            classification_fourth = getdata(data.classification_fourth),
            remark = getdata(data.remark)
        )
        print medicine.name
        print 'count: ',i
        medicine.save()

